<?php

/**
 * @file classes/journal/CTKTDAO.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class CTKTDAO
 * @ingroup journal
 * @see CTKT
 *
 * @brief Operations for retrieving and modifying CTKT objects.
 */

// $Id$
import ( 'classes.acct.CTKT' );
class CTKTDAO extends DAO {
	function &getCTKT_view($query, $paramArray) {
		//echo $query;
		$result = & $this->retrieve ( $query, $paramArray );

		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_view_FromRow' );
		
		$sections = $returner;
		$i = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [$i] ['idphieu'] = $section->getidphieu ();
			$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			//$sections [$i] ['noco'] = $section->getnoco ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['macttkdu'] = $section->getmacttkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['sotienvn'] = $section->getsotienvn ();
			$i += 1;
			unset ( $section );
		}
		return $sections;
	}



	function &_returnCTKT_view_FromRow(&$row) {
		$CTKT = new CTKT();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setdiengiai($row['diengiai']);
		$CTKT->settk($row['tk']);
		//$CTKT->setnoco($row['noco']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		return $CTKT;
	}


	function &getCTKT_viewNT($query, $paramArray) {
		//echo $query;
		$result = & $this->retrieve ( $query, $paramArray );

		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_viewNT_FromRow' );
		
		$sections = $returner;
		$i = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [$i] ['idphieu'] = $section->getidphieu ();
			$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['macttkdu'] = $section->getmacttkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['sotienvn'] = $section->getsotienvn ();
			$sections [$i] ['sotiennt'] = $section->getsotiennt ();
			$i += 1;
			unset ( $section );
		}
		return $sections;
	}

	function &_returnCTKT_viewNT_FromRow(&$row) {
		$CTKT = new CTKT();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setdiengiai($row['diengiai']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		$CTKT->setsotiennt($row['sotiennt']);
		return $CTKT;
	}


	function &getCTKT_edit($query) {
		$result = & $this->retrieve( $query );
		
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_edit_FromRow' );
		
		$sections = $returner;
		$i = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [$i] ['idphieu'] = $section->getidphieu ();	$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			$sections [$i] ['hoten'] = $section->gethoten ();
			$sections [$i] ['diachi'] = $section->getdiachi ();
			$sections [$i] ['lydo'] = $section->getlydo ();
			$sections [$i] ['solgctg'] = $section->getsolgctg ();
			$sections [$i] ['mant'] = $section->getmant ();
			$sections [$i] ['tigia'] = $section->gettigia ();
			$sections [$i] ['noco'] = $section->getnoco ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['macttkdu'] = $section->getmacttkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['sotienvn'] = $section->getsotienvn ();
			$sections [$i] ['sotiennt'] = $section->getsotiennt ();
			$i += 1;
			unset ( $section );
		}
		return $sections;
	}

	function &_returnCTKT_edit_FromRow(&$row) {
		$CTKT = new CTKT();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->sethoten($row['hoten']);
		$CTKT->setdiachi($row['diachi']);
		$CTKT->setlydo($row['lydo']);
		$CTKT->setsolgctg($row['solgctg']);
		$CTKT->setmant($row['mant']);
		$CTKT->settigia($row['tigia']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		$CTKT->setsotiennt($row['sotiennt']);
		$CTKT->setdiengiai($row['diengiai']);
		return $CTKT;

	}


function &getCTKT_BCTMTG_v2($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {
		$sections = $returner;
		$query = "SELECT dknovn, dknont FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%' && MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		while (!$result->EOF) {
			$rows = $result->GetRowAssoc(false);
			$sections [0] ['sodu']  = floatval($row['dknovn']);
			$sections [0] ['sodunt']  = floatval($row['dknont']);
			$sections [0] ['psco'] = "";
			$sections [0] ['psno'] = "";

			$sections [0] ['pscont'] = "";
			$sections [0] ['psnont'] = "";
			$result->moveNext();

		}
		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";
		
		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'N'	&& MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyno=$result->GetRowAssoc(false);
		$daukynont =floatval($daukyno["sum(b.sotiennt)"]);
		$daukynovn =floatval($daukyno["sum(b.sotienvn)"]);
		
		$sections [0] ['sodu']  += $daukynovn ;
		$sections [0] ['sodunt']  += $daukynovn ;

		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'C'	and MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyco=$result->GetRowAssoc(false);
		$daukycont = floatval($daukyco["sum(b.sotiennt)"]);
		$daukycovn = floatval($daukyco["sum(b.sotienvn)"]);
		$sections [0] ['sodu']  -=$daukycovn ;
		$sections [0] ['sodunt']  -=$daukycont ;
		if ($sections [0] ['sodu']   == 0)
			$sections [0] ['sodu']   ="";

		if ($sections [0] ['sodunt']   == 0)
			$sections [0] ['sodunt']   ="";

		$query = "SELECT a.soct, a.ngct, b.diengiai, a.mant, a.tigia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, b.macttkdu AS madt, a.mact, a.noco FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'and MANT like '".$mant. "%'";
		$query = $query . "		ORDER BY DATE_FORMAT(ngct, '%Y %m %d'), soct";

		$result = & $this->retrieve ( $query );


		$i = 1;
		$spsno = 0;
		$spsco = 0;

		if ($mant == "VND")
		{
			while (!$result->EOF) {
				$rows = $result->GetRowAssoc(false);
				$sections [$i] ['ngct'] = $rows['ngct'];;
				$sections [$i] ['soct'] = $rows['soct'];;
				$sections [$i] ['diengiai'] = $rows['diengiai'];;
				$sections [$i] ['tkdu'] = $rows['tkdu'];;
				$sections [$i] ['tk'] = $rows['tk'];;
				$sections [$i] ['noco'] = $rows['noco'];;
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($rows['sotienvn']);
					$sections [$i] ['psco'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($rows['psno']) ;
					$spsno  += floatval($sections [$i] ['psno']) ;

		
				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psco'] = floatval($rows['sotienvn']);
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
				}

				$i += 1;
				unset ( $section );
				$result->moveNext();
			}
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'];
			$sections [$i] ['soct'] = "";
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['diengiai'] ="Tổng phát sinh";
		}
		else
		{
			$spsnont = 0;
			$spscont = 0;
			while (!$result->EOF) {
				$rows = $result->GetRowAssoc(false);
				$sections [$i] ['ngct'] = $rows['ngct'];;
				$sections [$i] ['soct'] = $rows['soct'];;
				$sections [$i] ['diengiai'] = $rows['diengiai'];;
				$sections [$i] ['tkdu'] = $rows['tkdu'];;
				$sections [$i] ['tk'] = $rows['tk'];;
				$sections [$i] ['noco'] = $rows['noco'];;
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($rows['sotienvn']);
					$sections [$i] ['psnont'] = floatval($rows['sotiennt']);
					$sections [$i] ['psco'] = "";
					$sections [$i] ['pscont'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($rows['psno']) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] + floatval($rows['psnont']) ;
					$spsno  += floatval($sections [$i] ['psno']) ;
					$spsnont  += floatval($sections [$i] ['psnont']) ;


		
				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psnont'] = "";
					$sections [$i] ['psco'] = floatval($rows['sotienvn']);
					$sections [$i] ['pscont'] = floatval($rows['sotiennt']);
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] - floatval($sections [$i] ['pscont']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
					$spscont  += floatval($sections [$i] ['pscont']) ;
				}

				$i += 1;
				unset ( $section );
				$result->moveNext();
			}
		
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'];
			$sections [$i] ['soct'] = "";
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			if ($spsnont >0)
				$sections [$i] ['psnont'] = $spsnont;
			else
				$sections [$i] ['psnont'] = "";
			if ($spscont >0)
				$sections [$i] ['pscont'] = $spscont;
			else
				$sections [$i] ['pscont'] = "";


			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['sodunt']  = $sections [$i-1] ['sodunt']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";

		}
		return $sections;
	}	
	
function &getCTKT_BCTMTG($journalid, $nam, $ngay1, $ngay2, $tk, $mant) {

		$sections = array();//$returner;
		$query = "SELECT dknovn, dknont FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%' && MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$sodu=$result->GetRowAssoc(false);

		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";
		{
			$sections [0] ['sodu']  = floatval($sodu['dknovn']);
			$sections [0] ['sodunt']  = floatval($sodu['dknont']);
			$sections [0] ['psco'] = "";
			$sections [0] ['psno'] = "";

			$sections [0] ['pscont'] = "";
			$sections [0] ['psnont'] = "";

		}
		
		// tinh cac ps dau ky:
		echo '<script type="text/javascript">alert("blala tinh cac ps dau ky"); </script>';
		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'N'	&& MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyno=$result->GetRowAssoc(false);
		$daukynont =floatval($daukyno["sum(b.sotiennt)"]);
		$daukynovn =floatval($daukyno["sum(b.sotienvn)"]);
		
		$sections [0] ['sodu']  += $daukynovn ;
		$sections [0] ['sodunt']  += $daukynovn ;

		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'C'	and MANT like '".$mant. "%'";
		$result = & $this->retrieve ( $query );
		$daukyco=$result->GetRowAssoc(false);
		$daukycont = floatval($daukyco["sum(b.sotiennt)"]);
		$daukycovn = floatval($daukyco["sum(b.sotienvn)"]);
		$sections [0] ['sodu']  -=$daukycovn ;
		$sections [0] ['sodunt']  -=$daukycont ;
		if ($sections [0] ['sodu']   == 0)
			$sections [0] ['sodu']   ="";

		if ($sections [0] ['sodunt']   == 0)
			$sections [0] ['sodunt']   ="";

		$query = "SELECT a.soct, a.ngct, b.diengiai, a.mant, a.tigia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, b.macttkdu AS madt, a.mact, a.noco FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'and MANT like '".$mant. "%'";
		$query = $query . "		ORDER BY ngct, soct";

		$result = & $this->retrieve ( $query );
		echo '<script type="text/javascript">alert("blala getsetting"); </script>';
		$i = 1;
		$spsno = 0;
		$spsco = 0;
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_BCTMTG_FromRow' );
		

		if ($mant == "VND")
		{
			while ( ($section = & $sectionsIterator->next ()) ) {
				$sections [$i] ['ngct'] = $section->getngct ();
				$sections [$i] ['soct'] = $section->getsoct ();
				$sections [$i] ['diengiai'] = $section->getdiengiai ();
				$sections [$i] ['tkdu'] = $section->gettkdu ();
				$sections [$i] ['tk'] = $section->gettk ();
				$sections [$i] ['noco'] = $section->getnoco ();
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($section->getsotienvn ());
					$sections [$i] ['psco'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($section->getsotienvn ()) ;
					$spsno  += floatval($sections [$i] ['psno']) ;

				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psco'] = floatval($section->getsotienvn ());
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
				}

				$i += 1;
				unset ( $section );
			}
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'] +1;

			$sections [$i] ['soct'] = "zzzzzz";
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";
			var_dump($sections);
		}
		else
		{
			$spsnont = 0;
			$spscont = 0;
			while ( ($section = & $sectionsIterator->next ()) ) {
				$sections [$i] ['ngct'] = $section->getngct ();
				$sections [$i] ['ngayct'] = $section->getngayct ();
				$sections [$i] ['soct'] = $section->getsoct ();
				$sections [$i] ['diengiai'] = $section->getdiengiai ();
				$sections [$i] ['tkdu'] = $section->gettkdu ();
				$sections [$i] ['tk'] = $section->gettk ();
				$sections [$i] ['noco'] = $section->getnoco ();
				if ($sections [$i] ['noco'] =='N')
				{
					$sections [$i] ['psno'] = floatval($section->getsotienvn ());
					$sections [$i] ['psnont'] = floatval($section->getsotiennt ());
					$sections [$i] ['psco'] = "";
					$sections [$i] ['pscont'] = "";
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + floatval($section->getsotienvn ()) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] + floatval($section->getsotiennt ()) ;
					$spsno  += floatval($sections [$i] ['psno']) ;
					$spsnont  += floatval($sections [$i] ['psnont']) ;


		
				}
				else
				{
					$sections [$i] ['psno'] = "";
					$sections [$i] ['psnont'] = "";
					$sections [$i] ['psco'] = floatval($section->getsotienvn ());
					$sections [$i] ['pscont'] = floatval($section->getsotiennt ());
					$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - floatval($sections [$i] ['psco']) ;
					$sections [$i] ['sodunt'] = $sections [$i-1] ['sodunt'] - floatval($sections [$i] ['pscont']) ;
					$spsco  += floatval($sections [$i] ['psco']) ;
					$spscont  += floatval($sections [$i] ['pscont']) ;
				}

				$i += 1;
				unset ( $section );
			}
		
			$sections [$i] ['ngct'] = date("Y m d");
			$sections [$i] ['ngayct'] = date("Y m d")+1;
			if ($spsno >0)
				$sections [$i] ['psno'] = $spsno;
			else
				$sections [$i] ['psno'] = "";
			if ($spsco >0)
				$sections [$i] ['psco'] = $spsco;
			else
				$sections [$i] ['psco'] = "";

			if ($spsnont >0)
				$sections [$i] ['psnont'] = $spsnont;
			else
				$sections [$i] ['psnont'] = "";
			if ($spscont >0)
				$sections [$i] ['pscont'] = $spscont;
			else
				$sections [$i] ['pscont'] = "";


			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['sodunt']  = $sections [$i-1] ['sodunt']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";

		}
		return $sections;
	}	

	function &zgetCTKT_BCTMTG($journalid, $nam, $ngay1, $ngay2, $tk) {
		$sections = $returner;
		

		$query = "SELECT dknovn FROM SDTT" .$journalid. $nam ;
		$query = $query . " WHERE MATK like '" . $tk . "%'";
		$result = & $this->retrieve ( $query );
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnSDTT_FromRow' );
		$sections [0] ['diengiai']  = "Số dư đầu kỳ:";
		while ( ($section = & $sectionsIterator->next ()) ) {
			$sections [0] ['sodu']  = intval($section->getdknovn());
			$sections [0] ['psco'] = "";
			$sections [0] ['psno'] = "";

		}
		// tinh so dau ky
		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'N'	";
		$result = & $this->retrieve ( $query );
		$daukyno=$result->GetRowAssoc(false);
		$daukynont =floatval($daukyno["sum(b.sotiennt)"]);
		$daukynovn =floatval($daukyno["sum(b.sotienvn)"]);
		
		$sections [0] ['sodu']  += $daukynovn ;

		$query =  "SELECT sum( b.sotiennt), sum(b.sotienvn) FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt".$journalid. $nam . " b  ON (a.idphieu = b.idphieu) WHERE DATE( a.ngct ) < '". $ngay1.  "' && a.TK like '" . $tk . "%' && a.noco = 'C'	";
		$result = & $this->retrieve ( $query );
		$daukyco=$result->GetRowAssoc(false);
		$daukycont = floatval($daukyco["sum(b.sotiennt)"]);
		$daukycovn = floatval($daukyco["sum(b.sotienvn)"]);
		$sections [0] ['sodu']  -=$daukycovn ;

		$query = "SELECT a.soct, a.ngct, b.diengiai, a.mant, a.tigia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn, b.macttkdu AS madt, a.mact, a.noco FROM CTKT".$journalid. $nam . " a LEFT JOIN ctktdt" .$journalid. $nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.TK like '" . $tk . "%'";
		$query = $query . "		ORDER BY ngct, soct";

		$result = & $this->retrieve ( $query );

		
		$sectionsIterator = new DAOResultFactory ( $result, $this, '_returnCTKT_BCTMTG_FromRow' );
		

		$i = 1;
		$spsno = 0;
		$spsco = 0;
		while ( ($section = & $sectionsIterator->next ()) ) {
			
			$sections [$i] ['ngct'] = $section->getngct ();
			$sections [$i] ['soct'] = $section->getsoct ();
			$sections [$i] ['diengiai'] = $section->getdiengiai ();
			$sections [$i] ['tkdu'] = $section->gettkdu ();
			$sections [$i] ['tk'] = $section->gettk();
			$sections [$i] ['noco'] = $section->getnoco ();
			if ($sections [$i] ['noco'] =='N')
			{
				$sections [$i] ['psno'] = $section->getsotienvn ();
				$sections [$i] ['psco'] = "";
				$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] + intval($sections [$i] ['psno']) ;
				$spsno  += intval($sections [$i] ['psno']) ;

	
			}
			else
			{
				$sections [$i] ['psno'] = "";
				$sections [$i] ['psco'] = $section->getsotienvn ();
				$sections [$i] ['sodu'] = $sections [$i-1] ['sodu'] - intval($sections [$i] ['psco']) ;
				$spsco  += intval($sections [$i] ['psco']) ;
			}

			$i += 1;
			unset ( $section );
		}
			$sections [$i] ['ngct'] = $sections [$i-1] ['ngct'];
			$sections [$i] ['soct'] = "";
			//$sections [$i] ['tkdu'] = "";
			//$sections [$i] ['tk'] = "";
			//$sections [$i] ['noco'] = "N";
			$sections [$i] ['psno'] = $spsno;
			$sections [$i] ['psco'] = $spsco;
			

			$sections [$i] ['sodu']  = $sections [$i-1] ['sodu']  ;
			$sections [$i] ['diengiai'] ="Số dư cuối kỳ";

		return $sections;
	}



	function &_returnCTKT_BCTMTG_FromRow(&$row) {
		$CTKT = new CTKT();
		$CTKT ->setidphieu($row['idphieu']);
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->setngayct($row['ngayct']);
		$CTKT->sethoten($row['hoten']);
		$CTKT->setdiachi($row['diachi']);
		$CTKT->setlydo($row['lydo']);
		$CTKT->setsolgctg($row['solgctg']);
		$CTKT->setmant($row['mant']);
		$CTKT->settigia($row['tigia']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setmacttkdu($row['macttkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		$CTKT->setsotiennt($row['sotiennt']);
		$CTKT->setdiengiai($row['diengiai']);
		$CTKT->setnoco($row['noco']);
		return $CTKT;

	}


	function &getCTKT_ThuChi($query) {
		$result = & $this->retrieve ( $query );
		$sections = $returner ;
		$i = 0;
		while (!$result->EOF) {
			$rows = $result->GetRowAssoc(false);
			$pmatk = $rows['matk'];
			$ptentk = $rows['tentk'];
			$sections[$pmatk."-".$ptentk] = $pmatk."-".$ptentk;

			if ($i==0)
			{
				$sections [0] ['ngct'] = $rows['ngct'] ;
				$sections [0] ['soct'] = $rows['soct'] ;
				$sections [0] ['hoten'] = $rows['hoten'] ;
				$sections [0] ['diachi'] = $rows['diachi'] ;
				$sections [0] ['lydo'] = $rows['lydo'] ;
				$sections [0] ['solgctg'] = $rows['solgctg'] ;
				$sections [0] ['tkdu'] = $rows['tkdu'] ;
				$sections [0] ['tk'] = $rows['tk'] ;
				$sections [0] ['sotienvn'] = floatval($rows['sotienvn']) ;
			}
			else
			{
				$sections [$i] ['tkdu'] = $rows['tkdu'];
				$cotkdu = 0;
				for ($x=0; $x <$i; $x++) {
					if ($sections [$i] ['tkdu'] != $sections [$x] ['tkdu'])
					{
						$cotkdu = 1;
					}
										
				} 
				if ($cotkdu == 1)
					$sections [0] ['tkdu'] .= "-" .  $rows['tkdu'];

				$sections [0] ['sotienvn'] += floatval($rows['sotienvn']) ;


			}
			$i += 1;

			unset($section);
			$result->moveNext();
		}
 	   return $sections;
	}

	function &_returnCTKT_ThuChi_FromRow(&$row) {
		$CTKT = new CTKT();
		$CTKT->setsoct($row['soct']);
		$CTKT->setngct($row['ngct']);
		$CTKT->sethoten($row['hoten']);
		$CTKT->setdiachi($row['diachi']);
		$CTKT->setlydo($row['lydo']);
		$CTKT->setsolgctg($row['solgctg']);
		$CTKT->settk($row['tk']);
		$CTKT->settkdu($row['tkdu']);
		$CTKT->setsotienvn($row['sotienvn']);
		return $CTKT;

	}

	function &_returnSDTT_FromRow(&$row) {
		$CTKT = new CTKT();
		$CTKT->setdknovn($row['dknovn']);
		$CTKT->setdknont($row['dknont']);
				return $CTKT;

	}

	




}

?>
